package com.example.permission.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.permission.dao.ResourceMapper;
import com.example.permission.service.ResourceService;
import com.example.platformsharedcommon.dict.resource.UserPlatform;
import com.example.platformsharedcommon.entity.permission.Resource;
import com.example.platformsharedcommon.entity.permission.ResourceExample;
import com.example.platformsharedcommon.vo.resource.ResourceVo;

@Service
public class ResourceServiceImpl implements ResourceService {
	@Autowired
	private ResourceMapper resourceMapper;

	@Override
	public List<Map<String, Object>> allResources() {
		List<ResourceVo> manageResource = listResourceByResplatform(UserPlatform.MANAGE.getId());
		List<ResourceVo> shopResource = listResourceByResplatform(UserPlatform.SHOP.getId());
		Map<String, Object> mangeMap = new HashMap<>();
		Map<String, Object> pidMap = new HashMap<>();
		mangeMap.put("id", 0);
		mangeMap.put("title", "后台根目录");
		mangeMap.put("resplatform", 1);
		mangeMap.put("list", manageResource);
		pidMap.put("id", 0);
		pidMap.put("title", "前台根目录");
		pidMap.put("resplatform", 2);
		pidMap.put("list", shopResource);
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(mangeMap);
		list.add(pidMap);
		return list;
	}

	@Override
	public List<ResourceVo> listResourceByResplatform(Integer resplatform) {
		List<ResourceVo> reuslt = new ArrayList<>();
		// 先把所有的资源获取到
		List<Resource> allResources = resourceMapper.selectByExample(new ResourceExample());
		// 先获取顶级资源
		List<Resource> pidResource = listResourceByPid(allResources, 0, resplatform);
		for (Resource resource : pidResource) {
			ResourceVo wrapResource = wrapResource(resource);
			// 根据顶级资源获取子集资源
			List<ResourceVo> listResourceVo = listResourceVo(wrapResource, null, allResources, false, resplatform);
			Collections.sort(listResourceVo);
			wrapResource.setChildren(listResourceVo);
			reuslt.add(wrapResource);
		}
		return reuslt;
	}

	/**
	 * 根据顶级资源获取子集资源
	 * 
	 * @param resource
	 * @param allResources
	 * @param resplatform
	 * @return
	 */
	private List<ResourceVo> listResourceVo(ResourceVo resource, List<Resource> roleList, List<Resource> allResources,
			boolean falg, Integer resplatform) {

		List<Resource> listResourceByPid = listResourceByPid(allResources, resource.getId(), resplatform);
		if (falg) {
			listResourceByPid.retainAll(roleList);
		}
		List<ResourceVo> childrenVo = new ArrayList<>();
		for (Resource resource2 : listResourceByPid) {
			ResourceVo wrapResource = wrapResource(resource2);
			List<ResourceVo> children = listResourceVo(wrapResource, roleList, allResources, falg, resplatform);
			wrapResource.setChildren(children);
			childrenVo.add(wrapResource);
		}
		// 设置子列表
		resource.setChildren(childrenVo);
		return childrenVo;
	}

	/**
	 * 包装资源
	 * 
	 * @param res
	 * @param btnRoles
	 * @param children
	 * @return
	 */
	private ResourceVo wrapResource(Resource res) {
		ResourceVo vo = new ResourceVo();
		vo.setId(res.getId());
		vo.setParentId(res.getParentId());
		vo.setType(res.getResType());
		vo.setTitle(res.getResTitle());
		vo.setSort(res.getSort());
		vo.setUrl(res.getResUrl());
		vo.setIcon(res.getIcon());
		vo.setPerms(res.getPerms());
		vo.setResPlatform(res.getResPlatform());
		return vo;
	}

	/**
	 * 通过父级id和平台获取子集资源 这样写公用性比较强 //
	 * 
	 * @param resourceVos
	 * @param pid
	 * @param resplatform
	 * @return
	 */
	private List<Resource> listResourceByPid(List<Resource> resourceVos, Integer pid, Integer resplatform) {
		// 接收根据pid获取的信息
		List<Resource> parentList = new ArrayList<>();
		// 接收根据resplatform获取的信息
		List<Resource> platfromList = new ArrayList<>();
		for (Resource resource : resourceVos) {
			if (resource.getParentId() != null && resource.getParentId() == pid) {
				parentList.add(resource);
			}
			if (resource.getResPlatform() != null && resource.getResPlatform() == resplatform) {
				platfromList.add(resource);
			}
		}
		if (pid != null && resplatform == null) {
			return parentList;
		}
		if (pid == null && resplatform != null) {
			return platfromList;
		}
		if (pid != null && resplatform != null) {
			// 只保存这个2个集合相同的数据
			parentList.retainAll(platfromList);
		}
		return parentList;
	}

	@Override
	public List<ResourceVo> resourceByRole(String roleIds, Integer resPlatform) {
		String[] split = roleIds.split(",");
		List<Resource> roleList=new ArrayList<>();
		for (int i = 0; i < split.length; i++) {
			// 根据角色获取到所有的 资源信息
		List<Resource>	list = resourceMapper.listRoleResource(split[i]);
			for (Resource resource : list) {
				roleList.add(resource);
			}
		}
		List<ResourceVo> result = new ArrayList<>();
		List<Resource> allReources = resourceMapper.selectByExample(new ResourceExample());
		List<Resource> list2 = listResourceByPid(allReources, 0, resPlatform);
		list2.retainAll(roleList);
		for (Resource resource : list2) {
			ResourceVo wrapResource = wrapResource(resource);
			List<ResourceVo> listResourceVo = listResourceVo(wrapResource, roleList, allReources, true, resPlatform);
			Collections.sort(listResourceVo);
			wrapResource.setChildren(listResourceVo);
			result.add(wrapResource);
		}

		return result;
	}
}
